package com.yunshang.shoppingmall.controller;

import com.yunshang.shoppingmall.common.MessageConstants;
import com.yunshang.shoppingmall.common.Response;
import com.yunshang.shoppingmall.entity.Classify;
import com.yunshang.shoppingmall.service.ClassifyService;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class ClassifyController {
    @Autowired
    ClassifyService classifyService;
    //分类页面
    @RequestMapping("/getClassifyList")
    public String getClassifyList(Model map){
        List<Classify> classifyList =classifyService.selectListAll();
        map.addAttribute("classifyList",classifyList);
        return "classify/classifyList2";
    }
    //增加节点页面
    @RequestMapping("/addClassify")
    public String addClassify(){
        return "classify/addClassify";
    }

    @ResponseBody
    @RequestMapping("/deleteClassify")
    //删除节点
    public Response deleteClassify(int classId)
     {
        List<Classify> classifyList=classifyService.selectList(classId);
        if(classifyList.size()==0) {
           int res2 = classifyService.deleteByPrimaryKey(classId);
            return new Response(MessageConstants.MSG_SUCCESS,res2,"删除成功");
        }
        else
        {
            return new Response(MessageConstants.MSG_OPERATE_FAILED,-1,"请删除下面接点再试！ ");
        }
    }

    //更新页面
    @RequestMapping("/editClassify")
    public String updateClassify( String classifyId){
        int classifyIds =Integer.valueOf(classifyId);
        Classify classify=classifyService.selectByPrimaryKey(classifyIds);
        return "editClassify";
    }
    @ResponseBody
    @RequestMapping("/addOrUpdateClassify")
    public Response addOrUpdateClassify(String classifyName, int upperRankId, int classId) {
        int res;
        Classify classify = new Classify();
        if(classId!=0)
        {
            classify=classifyService.selectByPrimaryKey(classId);
            classify.setClassifyName(classifyName);
            res= classifyService.updateByPrimaryKey(classify);
            return new Response(MessageConstants.MSG_OPERATE_FAILED,res,"");
        }
        else
        {
            classify.setClassifyName(classifyName);
            classify.setClassStatus("0");
            classify.setSort(1);
            classify.setUpperRankId(upperRankId);
            res = classifyService.insert(classify);
            return new Response(MessageConstants.MSG_OPERATE_FAILED,classify.getClassId(),classify.getUpperRankId().toString());
        }
    }

    @ResponseBody
    @RequestMapping("/updateUpperRankId")
    //调整节点
    public Response updateUpperRankId(Integer classId,Integer upperRankId)
    {
        System.out.println(classId+"-----"+upperRankId);
       int res=classifyService.updateUpperRankId(classId,upperRankId);
        if(res>0) {

            return new Response(MessageConstants.MSG_SUCCESS,res,"调整成功");
        }
        else
        {
            return new Response(MessageConstants.MSG_OPERATE_FAILED,-1,"调整失败");
        }
    }


    /**
     * 查询一级分类
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectYJClassify")
    public List<Classify> selectYJClassify(){
        List<Classify> classifyList=classifyService.selectYJClassify();
        return classifyList;
    }

    /**
     * 查询二级分类
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectRJClassify")
    public List<Classify> selectRJClassify( String classId) {
        List<Classify> classifyList = null;
        if (classId != "" && classId != null) {
            int classIds = Integer.parseInt(classId);
            classifyList = classifyService.selectRJClassify(classIds);
            return classifyList;
        }
        return classifyList;
    }

    /**
     * 查询三级分类
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectSJClassify")
    public List<Classify> selectSJClassify( String classId) {
        List<Classify> classifyList = null;
        if (classId != "" && classId != null) {
            int classIds = Integer.parseInt(classId);
            classifyList = classifyService.selectRJClassify(classIds);
            return classifyList;
        }
        return classifyList;
    }

}
